import Model from './abstract';

export default class ModelStore extends Model {
      
      constructor() {
            super();
            this.mainTable = 'store';
            this.versionName = 'version';
            this.dateAddedName = 'date_added';
            this.lastUpdatedName = 'last_updated';
      }

      /**
       * 获取store所有key数组
       */
      async getKeys() {
            let keys = [];
            let rows = await this.fetchOne("SELECT `key` FROM `store`");
            for (row of rows) {
                  let key = row.key;
                  keys.push(key);
            }
            return keys;
      }

      /**
       * 检测key是否存在
       */
      async hasKey(key) {
            return await this.numRows(this.mainTable, {key: key});
      }

      /**
       * 通过key保存数据
       */
      async saveByKey(data, key = null) {
            if (key !== null) {
                  if (await this.hasKey(key)) {
                        if (this.lastUpdatedName !== null) {
                              data[this.lastUpdatedName] = this.getNowtime();
                        }
                        await this.update(this.mainTable, data, {key: key});
                  } else {
                        data[key] = key;
                        if (this.dateAddedName !== null) {
                              data[this.dateAddedName] = this.getNowtime();
                        }
                        await this.insert(this.mainTable, data);
                  }
                  return key;
            }
            return false;
      }

      /**
       * 通过key删除对应的配置数据
       */
      async removeByKey(key) {
            if (key !== null) {
                  await this.delete(this.mainTable, {key: key});
                  return key;
            }
            return false;
      }

      /**
       * 通过key获取当前数据的version值
       */
      async getVersionByKey(key) {
            let row = await this.fetchOne(sprintf("SELECT `%s` as version FROM `%s` WHERE `key` = ? LIMIT 0, 1", 
                  this.versionName, this.mainTable),
                  [key]
            );
            if (row && row.version) {
                  return row.version;
            }
            return 0;
      }
      

      /**
       * 数据导入
       */
      async download(json = {}) {
            let remoteKeys = [];
            if (typeof(json) == 'object') {
                  let keys = Object.keys(json);
                  for (key of keys) {
                        remoteKeys.push(key);
                        let row = json[key];
                        let type = row.type;
                        let value = row.value;
                        if (row.type == 'image' && value !== null) {
                              let name = sprintf("store/%d", key);
                              let file = await downloadFile(value, name);
                              if (file !== null) {
                                    let data = {
                                          key: key,
                                          value: name,
                                          type: type
                                    };
                                    let pk = await this.saveByKey(data, key);
                              }
                        } else {
                              let data = {
                                    key: key,
                                    value: value,
                                    type: type
                              };
                              let pk = await this.saveByKey(data, key);
                        }
                  }
            }

            let localKeys = await this.getKeys();
            let delKeys = localKeys.filter(el => !remoteKeys.includes(el));
            for (delKey of delKeys) {
                  if (delKey !== null) {
                        await this.removeByKey(delKey);
                  }
            }
            return true;
      }

      /**
       * 数据上传
       */
      async upload() {
            return true;
      }

}